package com.fire.market.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

//Spring容器内没有TomcatEmbeddedServletContainerFactory这个Bean时，会把此Bean加载进Spring容器中
@Configuration
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler();
                //定制化keepalivetimeout,设置30秒没有请求就断开
                protocolHandler.setKeepAliveTimeout(30000);
                //当客户端发送100000个请求则自动断开keepalive链接
                protocolHandler.setMaxKeepAliveRequests(100000);
            }
        });
    }
}
